﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKRevitDimensionText.Forms
{
	public partial class DlgDoorWindowDim : System.Windows.Forms.Form
	{
		public DlgDoorWindowDim()
		{
			this.InitializeComponent();
		}

		public int SelectWallType
		{
			get
			{
				return this.m_nSelectWallType;
			}
		}

		private void rdoWallDim1_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nSelectWallType = 0;
		}

		private void rdoWallDim2_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nSelectWallType = 1;
		}

		private void DlgDoorWindowsDim_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadDlgData();
			if (this.m_nDlgPosX < 0 || this.m_nDlgPosX > 3000)
			{
				this.m_nDlgPosX = 0;
			}
			if (this.m_nDlgPosY < 0 || this.m_nDlgPosY > 3000)
			{
				this.m_nDlgPosY = 0;
			}
			base.Location = new System.Drawing.Point(this.m_nDlgPosX, this.m_nDlgPosY);
			this.rdoSelectWallType1.Checked = (this.m_nSelectWallType == 0);
			this.rdoSelectWallType2.Checked = (this.m_nSelectWallType == 1);
		}

		private void DlgDoorWindowsDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			System.Drawing.Point location = base.Location;
			this.m_nDlgPosX = location.X;
			this.m_nDlgPosY = location.Y;
			this.WriteDlgData();
			this.CancelCommand();
		}

		private void DlgDoorWindowDim_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelCommand()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgDoorWindowDim.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgDoorWindowDim.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ReadDlgData()
		{
			string path = Path.GetTempPath() + "DoorWindowDim.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.m_nDlgPosX = Convert.ToInt32(streamReader.ReadLine());
			this.m_nDlgPosY = Convert.ToInt32(streamReader.ReadLine());
			this.m_nSelectWallType = Convert.ToInt32(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void WriteDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "DoorWindowDim.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_nDlgPosX);
			streamWriter.WriteLine(this.m_nDlgPosY);
			streamWriter.WriteLine(this.m_nSelectWallType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private int m_nDlgPosX;

		private int m_nDlgPosY;

		private int m_nSelectWallType;
	}
}
